package Client.TaggingSystem
import java.io.File
import java.nio.file.FileSystems
import java.net.URL
import java.net.URLClassLoader

/**
 * Class to manage the tagging system
 */
object TagManager {
  /**
   * Initialize a new TagMaker
   * @param tagger the path of the new class
   */
  def init (tagger:String){
    println("initializing tagger!")
    try {
      //load a class and set it as the tag maker
      val path=FileSystems.getDefault.getPath(tagger)
      val parent=path.getParent
      val file=new File(parent.toString)
      val url=file.toURL
      val urls=new Array[URL](1)
      urls.update(0, url)
      val classLoader=new URLClassLoader(urls)
      var classname=path.getFileName.toString
      var name=classname.substring(0, classname.lastIndexOf("."))
      val cls=classLoader.loadClass(name)
      tagMaker=cls.newInstance.asInstanceOf[TagMaker]
    }
    catch {
      case e:Exception => System.err.println(e.getMessage)
    }
  }
  
  /**
   * tag a file
   * @param file the file to tag
   */
  def tag (file:File){
    var tags=tagMaker.tag(file)
    for (t <- tags)
      DataBase.saveTag(file.toString, t)
  }
  var tagMaker = null.asInstanceOf[TagMaker]
  
  def delete(file:String){
    DataBase.delete(file.toString)
  }
}